package com.changgou.controller;

import com.alibaba.fastjson.JSON;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.service.WeiXinPayService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName WeiXinPayController
 * @Description
 * @Author 洛尘
 * @Date 18:34 2019/11/26
 * @Version 2.1
 **/
@RestController
@RequestMapping("/weixin/pay")
public class WeiXinPayController {

    @Autowired
    private WeiXinPayService weiXinPayService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private Environment env;

    /**
     *  创建二维码
     * @return
     */
    @RequestMapping("/create/netive")
    public Result createNetive(@RequestParam Map<String,String> parameters){
        Map<String ,String> map = weiXinPayService.createNetive(parameters);
        return new Result(true, StatusCode.OK,"创建二维码成功",map);
    }

    /**
     * 支付状态查询
     * @param out_trade_no
     * @return
     */
    @RequestMapping("/query/status")
    public Result queryStatus(String out_trade_no){
        Map<String,String> map= weiXinPayService.queryStatus(out_trade_no);
        return  new Result(true, StatusCode.OK,"查询状态成功",map);
    }

    /**
     * @author 洛尘
     * @Description 获取回调信息
     * @param 
     * @date 2019/11/29 16:36
     * @return 
    */
    @RequestMapping("/notify/url")
    public String notifyUrl(@RequestBody Map<String,String> map){
    /*    System.out.println("map = " + map);
        String attach = map.get("attach");
        Map<String, String> attachMap = JSON.parseObject(attach, Map.class);
        //Map<String, String> attachMap = new HashMap<>();
        String exchange = attachMap.get("exchange");
        String routing = attachMap.get("routing");*/
        rabbitTemplate.convertAndSend(env.getProperty("mq.pay.exchange.seckillorder"),
                env.getProperty("mq.pay.routing.seckillkey"), JSON.toJSONString(map));
        return "success";
    }

    /**
     *  获取回调数据
     * @param
     * @return
     */
//    @RequestMapping("/notify/url")
//    public String notifyUrl(HttpServletRequest request){
//        try {
//            //获取微信回调信息
//            ServletInputStream inputStream = request.getInputStream();
//            // 网络传输的字节流操作（内存操作）
//            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//            // 定义缓冲区
//            byte[] buffer = new byte[1024];
//            int len = 0;
//            while ((len = inputStream.read(buffer)) != -1){
//                byteArrayOutputStream.write(buffer,0,len);
//            }
//            byteArrayOutputStream.flush();
//            byteArrayOutputStream.close();
//            inputStream.close();
//            // 将字节转换成String
//            String strXML = new String(byteArrayOutputStream.toByteArray(), "UTF-8");
//            // 回调的数据
//            Map<String, String> map = WXPayUtil.xmlToMap(strXML);
//            // 将数据发送给mq
//            rabbitTemplate.convertAndSend(env.getProperty("mq.pay.exchange.order"), env.getProperty("mq.pay.routing.key"), JSON.toJSONString(map));
//            return "success";
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return "获取回调函数失败";
//    }
    

}
